import { asyncFormat } from '@/utils'
import * as API from './api'

export const exportTabel =  (API, filter) => async (options, param, tableEle) =>{

    const [ res, err ] = await asyncFormat(API({ ...param, page: 1, pageSize: 10000 }))
    if(err){console.error(err); return false}
    
    if(!res.length){ console.error("没有数据,无法导出"); return false }

    tableEle.exportCsv({
        original: false,
        ...options,
        columns: options.columns.filter(item => !item._unexport),
        data: filter ? filter(res, options, param) : res
    })
    
    return true
    
}


export const strFormatter = str => str ? str.replace(/,/g, " ") : ""


// 救援
export const exportJyTable = exportTabel(
    API.getRescueHistoryList, 
    (res) => res.map((item, index) =>{
        return {
            ...item,
            _index: index + 1,
            finish_type_name: strFormatter(item.finish_type_name),
            balance_type: item.balance_type == 0 ? "现结" : "月结",
        }
        
    })
)


// 放空
export const exportFkTable = exportTabel(
    API.getRescueHistoryList, 
    (res) => res.map((item, index) =>{
        return {
            ...item,
            _index: index + 1,
            operating_pic: strFormatter(item.operating_pic),
        }
    })
)


// 超时
export const exportCsTable = exportTabel(
    API.getRescueHistoryList, 
    (res) => res.map((item, index) =>{

        return {
            ...item,
            _index: index + 1,
        }
        
    })
)